#!/usr/bin/env python3
"""
使用PyInstaller交叉编译Windows可执行文件
需要安装Windows工具链
"""

import os
import sys
import subprocess
import platform

def check_wine():
    """检查是否安装了Wine"""
    try:
        result = subprocess.run(['wine', '--version'], capture_output=True, text=True)
        if result.returncode == 0:
            print("✓ Wine 已安装")
            return True
        else:
            print("❌ Wine 未安装或未配置")
            return False
    except FileNotFoundError:
        print("❌ Wine 未安装")
        return False

def install_wine():
    """安装Wine"""
    print("正在安装Wine...")
    try:
        if platform.system() == "Darwin":  # macOS
            subprocess.run(['brew', 'install', 'wine'], check=True)
            print("✓ Wine 安装成功")
            return True
        else:
            print("❌ 不支持的操作系统")
            return False
    except subprocess.CalledProcessError:
        print("❌ Wine 安装失败")
        return False

def build_windows_with_wine():
    """使用Wine构建Windows可执行文件"""
    print("开始使用Wine构建Windows可执行文件...")
    
    # 使用Wine运行PyInstaller
    cmd = [
        "wine", "python", "-m", "PyInstaller",
        "--onefile",
        "--windowed",
        "--name=Telegram群发软件",
        "--hidden-import=tkinter",
        "--hidden-import=tkinter.ttk",
        "--hidden-import=tkinter.messagebox",
        "--hidden-import=tkinter.filedialog",
        "--hidden-import=tkinter.scrolledtext",
        "--hidden-import=tkinter.simpledialog",
        "--hidden-import=telethon",
        "--hidden-import=asyncio",
        "--hidden-import=threading",
        "--hidden-import=queue",
        "--hidden-import=json",
        "--hidden-import=datetime",
        "--hidden-import=os",
        "--hidden-import=sys",
        "telegram_mass_sender.py"
    ]
    
    try:
        subprocess.run(cmd, check=True)
        print("✓ Windows可执行文件构建成功！")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ 构建失败: {e}")
        return False

if __name__ == "__main__":
    print("=" * 50)
    print("Telegram 群发软件 - Wine Windows 交叉编译")
    print("=" * 50)
    
    if not check_wine():
        print("正在安装Wine...")
        if not install_wine():
            print("\n请手动安装Wine: https://www.winehq.org/")
            sys.exit(1)
    
    if build_windows_with_wine():
        print("\n🎉 交叉编译完成！")
        print("Windows可执行文件位置: dist/Telegram群发软件.exe")
    else:
        print("\n❌ 交叉编译失败！")
        sys.exit(1) 